package com.itheima.boottlias.controller;

import com.itheima.boottlias.annotation.LogAnnotation;
import com.itheima.boottlias.domain.pojo.Dept;
import com.itheima.boottlias.domain.vo.Result;
import com.itheima.boottlias.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制器
 * @author ghy
 * @version 1.0
 * @since 2025-07-10 10:08
 */
@RestController
//表示所有的Handler都有这个URL作为前缀
@RequestMapping("/depts")
@Slf4j   //lombok集成了日志
public class DeptController {

    // private final Logger LOGGER = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    /**
     * 查询部门列表
     * @return Result(data放的是部门列表数据)
     */
    //@RequestMapping(method = RequestMethod.GET)
    @GetMapping
    public Result findAll(){
        // System.out.println("DeptController findAll");
        // log.info("DeptController findAll");
        //调用业务
        List<Dept> depts = deptService.findAll();
        return Result.success(depts);
    }

    @LogAnnotation
    @DeleteMapping
    public Result deleteById(@RequestParam(name = "id") Integer id){
        log.debug("DeptController deleteById {}", id);
        //调用业务
        deptService.deleteById(id);
        return Result.success();
    }

    @LogAnnotation
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("DeptController add {}", dept);
        deptService.add(dept);
        return Result.success();
    }

    // @GetMapping(value = "/{id}")
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable(name = "id") Integer id){
        log.info("DeptController findById {}", id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    @LogAnnotation
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("DeptController update {}", dept);
        deptService.updateById(dept);
        return Result.success();
    }


}
